package com.tanhua.server.service;

import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.autoconfig.template.SmsTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.model.domain.User;

import com.tanhua.model.vo.ErrorResult;
import com.tanhua.server.exception.BusinessException;
import org.apache.commons.codec.digest.DigestUtils;

import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserService {
    //发送短信验证码
    /**/
    /*1.生成随机的六位数验证码-->
     * 2.将验证码存入Redis中,设定有效时间
     * 3.远程调用发送短信的方法*/
    //因为将SmsTemplate自动装配,所以相当于自己的对象
    @Autowired
    private SmsTemplate smsTemplate;

    @Autowired
    private HuanXinTemplate huanXinTemplate;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private UserFreezeService userFreezeService;
    @DubboReference
    private UserApi userApi;
    @Autowired
    private MqMessageService mqMessageService;
    public void sendMsg(String phone) {
        User user = userApi.findByMobile(phone);
        if(user != null) {
            //状态为1表示禁止登陆
            userFreezeService.checkUserStatus("1",user.getId());
        }
        //1,生成验证码
        // String code = RandomStringUtils.randomNumeric(6);
        String code = "123456";
        //调用方法,发送短信验证码
       //  smsTemplate.sendSms(phone, code);
        //调用方法存进Redis中,设定存活时间
        //根据手机号查询用户，如果用户存在，判断是否被冻结


        redisTemplate.opsForValue().set("CHECK_CODE_" + phone, code, Duration.ofDays(1));
    }

    /**
     * 验证登录
     * @param phone
     * @param code
     */
    public Map loginVerification(String phone, String code) {
        //1、从redis中获取下发的验证码
        String redisCode = redisTemplate.opsForValue().get("CHECK_CODE_" + phone);
        //2、对验证码进行校验（验证码是否存在，是否和输入的验证码一致）
        if(StringUtils.isEmpty(redisCode) || !redisCode.equals(code)) {
            //验证码无效
            throw new BusinessException(ErrorResult.loginError());
        }
        //3、删除redis中的验证码
        redisTemplate.delete("CHECK_CODE_" + phone);
        //4、通过手机号码查询用户
        User user = userApi.findByMobile(phone);
        boolean isNew = false;
        //5、如果用户不存在，创建用户保存到数据库中
        String type = "0101";//登录
        if(user == null) {
            type = "0102";//注册
            user = new User();
            user.setMobile(phone);
            user.setPassword(DigestUtils.md5Hex("123456"));
            Long userId = userApi.save(user);
            user.setId(userId);
            isNew = true;

            //注册环信用户
            String hxUser = "hx"+user.getId();
            Boolean create = huanXinTemplate.createUser(hxUser, Constants.INIT_PASSWORD);
            if(create) {
                user.setHxUser(hxUser);
                user.setHxPassword(Constants.INIT_PASSWORD);
                userApi.update(user);
            }
        }

        //向rabbitmq发消息记录登陆/注册日志
        mqMessageService.sendLogService(user.getId(),type,"user",null);

        //6、通过JWT生成token(存入id和手机号码)
        Map tokenMap = new HashMap();
        tokenMap.put("id",user.getId());
        tokenMap.put("mobile",phone);
        String token = JwtUtils.getToken(tokenMap);
        //7、构造返回值
        Map retMap = new HashMap();
        retMap.put("token",token);
        retMap.put("isNew",isNew);

        return retMap;
    }
}
